package com.yanxu.controller;

import com.yanxu.common.SnmpParameter;
import com.yanxu.inter.SnmpRequestHandle;
import org.snmp4j.CommunityTarget;
import org.snmp4j.Snmp;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OctetString;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2023/11/9-2023
 * @Description: 这里用一句话描述这个类的作用
 */
@RestController
public class TestController {

    //Executor executor=new ThreadPoolExecutor(10,20,60, TimeUnit.SECONDS,new LinkedBlockingDeque<>());

    Executor executor=new ThreadPoolExecutor(
            10,
            20,60,
            TimeUnit.SECONDS,new LinkedBlockingDeque<>(),
            new ThreadPoolExecutor.CallerRunsPolicy());
    //定义了拒绝策略。使用 CallerRunsPolicy 拒绝策略。当线程池核心线程已经满了，队列也已经满了，最大的线程数量的线程也都在工作的时候，
    //说明线程已经全部都在繁忙的工作了，此时拒绝策略就会发挥作用。我们使用 CallerRunsPolicy 策略的方式。让提交任务的线程去执行相应的任务。

    @Autowired
    @Qualifier(value = "snmp-get")
    private SnmpRequestHandle snmpGetRequestHandle;

    @Autowired
    @Qualifier(value = "snmp-set")
    private SnmpRequestHandle snmpSetRequestHandle;

    @GetMapping("/testGetSnmp")
    public Object testGetSnmp() throws Exception {
        SnmpParameter snmpParameter = new SnmpParameter();
        snmpParameter.setIp("139.129.16.84");
        snmpParameter.setPort(161);
        snmpParameter.setVersion(1);
        snmpParameter.setCommunity("admin");
        //snmpParameter.setOids(new String[]{"1.3.6.1.2.1.25.2.2.0","1.3.6.1.2.1.1.1.0"});
        snmpParameter.setOids(new String[]{"1.3.6.1.2.1.1.2.0"});
        snmpGetRequestHandle.execute(snmpParameter);
        return null;
    }


    @GetMapping("/testSetSnmp")
    public Object testSetSnmp() throws Exception {
        SnmpParameter snmpParameter = new SnmpParameter();
        snmpParameter.setIp("localhost");
        snmpParameter.setPort(161);
        snmpParameter.setVersion(1);
        snmpParameter.setCommunity("cooqe");
        snmpParameter.setOids(new String[]{"1.3.6.1.2.1.1.4.0"});
        snmpParameter.setVariables(new String[]{"cooeq-kkqq"});
        snmpParameter.setTypes(new String[]{"OctetString"});
        snmpSetRequestHandle.execute(snmpParameter);
        return null;
    }


    @GetMapping("/loadSnmpMib")
    public Object loadSnmpMib() throws Exception {
        try {

            // 设置SNMP参数
            CommunityTarget target = new CommunityTarget();
            target.setCommunity(new OctetString("admin"));
            target.setAddress(GenericAddress.parse("udp:192.168.1.1/161"));
            target.setVersion(SnmpConstants.version2c);

            // 创建SNMP对象
            Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
            snmp.listen();


        }catch (Exception e){

        }
        return null;
    }
}